package com.register.service.impl;

import com.register.pojo.User;
import com.register.service.RegistorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class RegistorServiceImpl implements RegistorService {
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查看redis中是否有ticket
     *
     * @param ticket
     * @return
     */
    @Override
    public User verifyTicket(String ticket) {
        ValueOperations<String, User> operations = redisTemplate.opsForValue();
        if (redisTemplate.hasKey(ticket)) {
            return operations.get(ticket);
        }
        return null;
    }

    /**
     * 将user存入redis
     * @param user
     */
    public void setRedisUser(String ticket,User user){
        ValueOperations<String,User> operations=redisTemplate.opsForValue();
        operations.set(ticket,user,600, TimeUnit.SECONDS);
    }

    /**
     * 删除redis中登录的用户
     * @param ticket
     */
    @Override
    public void deleteUser(String ticket) {
        if(redisTemplate.hasKey(ticket))
            redisTemplate.delete(ticket);
    }
}
